package com.ruoyi.project.jiaqin.val.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.jiaqin.val.domain.ClassAttrVal;
import com.ruoyi.project.jiaqin.val.service.IClassAttrValService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 分类属性值Controller
 *
 * @author admin
 * @date 2020-12-21
 */
@Controller
@RequestMapping("/jiaqin/val")
public class ClassAttrValController extends BaseController
{
    private String prefix = "jiaqin/val";

    @Autowired
    private IClassAttrValService classAttrValService;

    @RequiresPermissions("jiaqin:val:view")
    @GetMapping()
    public String val()
    {
        return prefix + "/val";
    }

    /**
     * 查询分类属性值列表
     */
    @RequiresPermissions("jiaqin:val:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ClassAttrVal classAttrVal)
    {
        startPage();
        List<ClassAttrVal> list = classAttrValService.selectClassAttrValList(classAttrVal);
        return getDataTable(list);
    }

    /**
     * 导出分类属性值列表
     */
    @RequiresPermissions("jiaqin:val:export")
    @Log(title = "分类属性值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ClassAttrVal classAttrVal)
    {
        List<ClassAttrVal> list = classAttrValService.selectClassAttrValList(classAttrVal);
        ExcelUtil<ClassAttrVal> util = new ExcelUtil<ClassAttrVal>(ClassAttrVal.class);
        return util.exportExcel(list, "val");
    }

    /**
     * 新增分类属性值
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存分类属性值
     */
    @RequiresPermissions("jiaqin:val:add")
    @Log(title = "分类属性值", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ClassAttrVal classAttrVal)
    {
        System.out.println(classAttrVal);
        if(classAttrVal.getAttrKeyId() == null || classAttrVal.equals("")){
            return AjaxResult.error("属性名不可为空");
        }
        //根据分类ID查询属性编码列表
        List<Long> listCode = classAttrValService.selectAttrCodesByQClassId(classAttrVal.getQuotationClassId());
        for (Long l:listCode) {//循环比较防止属性编码重复
            if(l == classAttrVal.getAttrCode()){
                return AjaxResult.error("同一分类属性编码不可重复!");
            }
        }
        return toAjax(classAttrValService.insertClassAttrVal(classAttrVal));
    }

    /**
     * 修改分类属性值
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ClassAttrVal classAttrVal = classAttrValService.selectClassAttrValById(id);
        mmap.put("classAttrVal", classAttrVal);
        return prefix + "/edit";
    }

    /**
     * 修改保存分类属性值
     */
    @RequiresPermissions("jiaqin:val:edit")
    @Log(title = "分类属性值", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ClassAttrVal classAttrVal)
    {
        return toAjax(classAttrValService.updateClassAttrVal(classAttrVal));
    }

    /**
     * 删除分类属性值
     */
    @RequiresPermissions("jiaqin:val:remove")
    @Log(title = "分类属性值", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(classAttrValService.deleteClassAttrValByIds(ids));
    }
}
